//获取cookie
function getCookie() {
    let obj = {
        token:'',
        userName:'',
        userPwd:'',
    };
    let arr = document.cookie.split('; ');
    arr.forEach(item => {
        let arr2 = item.split('=');
        if (arr2[0] === 'token') {
            obj.token = arr2[1];
        }else if (arr2[0] === 'userName') {
            // 保存数据并赋值
            obj.userName = arr2[1];
        } else if (arr2[0] === 'userPwd') {
            obj.userPwd = arr2[1];
        }
    })
    return obj;
}
//设置cookie
function setCookie(obj,exdays) {
    let exdate = new Date();
    if(obj.token){
        exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays.token);
        window.document.cookie = 'token' + '=' + obj.token + ';path=/;expires=' + exdate.toUTCString();
    }else if(obj.username||obj.userPwd){
        exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays.username);
        window.document.cookie = 'userName' + '=' + obj.username + ';path=/;expires=' + exdate.toUTCString();
        window.document.cookie = 'userPwd' + '=' + obj.userpass + ';path=/;expires=' + exdate.toUTCString();
    }
}
//设置cookie的token
function clearCookie() {
    setCookie({token:'',username:'',userpass:''},{token:-1,username:-1,userpass:-1})
}

export default {
    getCookie,
    setCookie,
    clearCookie
}
